//
//  LQStatusRetweetView.swift
//  Weibo
//
//  Created by ios on 16/8/12.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
class LQStatusRetweetView: UIView {
//    //  记录转发微博视图的底部约束
    var retweetViewBottomConstraint: Constraint?
//
       //  使用ViewModel绑定数据
    var statusViewModel:LQStatusViewModel? {
        didSet{
          //  绑定转发微博内容
            contentLabel.text = statusViewModel!.retweetContent
            
            //  卸载上一次约束
            retweetViewBottomConstraint?.uninstall()
//          判断配图是否存在并且是否大于0
            if  let picUrs = statusViewModel?.status?.retweeted_status?.pic_urls where picUrs.count > 0 {
             //  显示配图
                pictureView.hidden = false
                //设置配图信息
                pictureView.picUrls = picUrs
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                   self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
            
            }else{
             //  隐藏配图
                pictureView.hidden = true
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
            
        }
    }
     //MARK: --懒加载控件
    //内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        return label
    }()

    //配图
    private lazy var pictureView:LQStatusPictureView = {
        let view = LQStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件 设置约束
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
    
        
//        设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
        make.leading.equalTo(contentLabel)
        }
        
        //  关键约束 让其转发微博的视图底部约束 = 内容底部的约束 + 间距
       self.snp_makeConstraints { (make) -> Void in
          self.retweetViewBottomConstraint =   make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
     }
}
